/**
 * Created by wangzhuoya on 2017/6/22.
 */

$('.time').daterangepicker({
		startDate: new Date(new Date().getTime()-7*1000*60*60*24),
		endDate: new Date(new Date().getTime()-1*1000*60*60*24),
		showDropdowns : true,
		showWeekNumbers : false,
		timePicker : false,
		timePicker12Hour : false,
		ranges : {
			/* '昨日': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
			 '过去7天': [moment().subtract(7, 'days'), moment().subtract(1, 'days')],
			 '过去31天': [moment().subtract(31, 'days'), moment().subtract(1, 'days')] */
		},
		opens : 'right',
		buttonClasses : [ 'btn btn-default' ],
		applyClass : 'btn-small btn-pink blue',
		cancelClass : 'btn-small',
		format : 'yyyy-MM-dd',
		separator : ' to ',
		locale : {
			applyLabel : '确定',
			cancelLabel : '取消',
			fromLabel : '起始时间',
			toLabel : '结束时间',
			customRangeLabel : '自定义',
			daysOfWeek : [ '日', '一', '二', '三', '四', '五',
				'六' ],
			monthNames : [ '一月', '二月', '三月', '四月', '五月',
				'六月', '七月', '八月', '九月', '十月', '十一月',
				'十二月' ],
			firstDay : 1
		}
	},
	function(start, end) {//格式化日期显示框
		$('.time').html( start.format('yyyy-MM-dd') + ' - '+ end.format('yyyy-MM-dd'));
	});

jQuery(function($) {
	$(".time").each(function () {
		changeButtonColor($(this).siblings("button").eq(1));
	});
});

function replaceDate(obj,dateNum){
	recoverButtonColor($(obj).siblings("button"));
	changeButtonColor(obj);
	if(dateNum == '1'){
		startDate = new Date(new Date().getTime()-1*1000*60*60*24);
		endDate = new Date(new Date().getTime()-1*1000*60*60*24);
	}else if(dateNum == '7'){
		startDate = new Date(new Date().getTime()-7*1000*60*60*24);
		endDate = new Date(new Date().getTime()-1*1000*60*60*24);
	}else if(dateNum == '31'){
		startDate = new Date(new Date().getTime()-31*1000*60*60*24);
		endDate = new Date(new Date().getTime()-1*1000*60*60*24);
	}
	var dateStr = dateFormat(startDate) + " - " + dateFormat(endDate);
	$(obj).siblings(".time").val(dateStr);
}

function dateFormat(date){
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = month + "/" + strDate + "/" + date.getFullYear();
	return currentdate;
}

function changeButtonColor(obj){
	$(obj).addClass("btn-info");
}

function recoverButtonColor(obj){
	$(obj).removeClass("btn-info");
}
var inputId;
$(".time").click(function(){
	inputId = $(this).attr("id");
});
$('.applyBtn').each(function(){
	$(this).click(function(){
		$("#"+inputId).siblings("button").removeClass("btn-info");
	});
})
